package com.wn.common.api;

import com.wn.common.api.status.IStatusCode;
import com.wn.common.api.status.impl.BusinessCode;
import com.wn.common.api.status.impl.ResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author yellow Docter
 * @date 2022 -09-02
 * @desc
 *  统一封装结果集
 */
@ApiModel("统一返回结果")
@Data
@NoArgsConstructor   //构造
@AllArgsConstructor
public class Result<T> {

    /**
     * Code 编码
     * Msg  信息
     * Data 数据
     */
    @ApiModelProperty("响应码")
    private Integer code;

    @ApiModelProperty("响应信息")
    private String  msg;

    @ApiModelProperty("响应数据")
    private T data;

    /**
     *  泛型方法可以使用static修饰
     *
     *  泛型方法的语法:
     *  修饰  <T> 泛型标识  返回结果  方法的名称(参数列表){
     *
     *      方法体;
     *
     *  }
     * @return
     */
    public static <T> Result<T> common(IStatusCode code,T data){
        return new Result<>(code.getCode(),code.getMessage(),data);
    }

    /**
     * ResultCode 枚举
     *
     *  ResultCode.SUCCESS(200,"操作成功"))
     *  ResultCode.SUCCESS.getCode()        拿到200的状态吗
     *  ResultCode.SUCCESS.getMessage()     拿到'操作成功' 成功的信息
     */
    public static <T> Result<T> ok(){
        return new Result<>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),null);
    }

    public static <T> Result<T> ok(String msg){
        return new Result<>(ResultCode.SUCCESS.getCode(),msg,null);
    }

    public static <T> Result<T> common(T data){
        return new Result<>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),data);
    }

    public static <T> Result<T> error(){
        return new Result<>(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage(),null);
    }

    public static <T> Result<T> error(T data){
        return new Result<>(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage(),data);
    }

    public static <T> Result<T> error(Integer code, String msg){
        return new Result<>(code,msg,null);
    }

    public static <T> Result<T> error(IStatusCode code, T data){
        return new Result<>(code.getCode(),code.getMessage(),null);
    }

    public static <T> Result<T> error(IStatusCode code){
        return new Result<>(code.getCode(),code.getMessage(),null);
    }

}
